package Shop.dao;

import Shop.damain.Users;
import Shop.tools.DBHelper;

import java.util.List;
import java.util.Map;

/**
 * Created by BadHuge on 2020/10/6.
 */
public class UserDaoImpl implements IUserDao {
    @Override
    public Users login(String username, String userpwd) {
        String sql="select * from Users where username = '"+ username +"'";
        List<Map<String,Object>> data = DBHelper.query(sql);
        if(!data.isEmpty()){
            Map<String,Object> row = data.get(0);
            if(userpwd.equals(row.get("userpwd").toString())){
                return new Users(Integer.parseInt(row.get("uid").toString())
                        ,row.get("username").toString()
                        ,row.get("userpwd").toString()
                        ,Integer.parseInt(row.get("gendar").toString())
                        ,row.get("uimage").toString()
                        ,Integer.parseInt(row.get("age").toString())
                        ,row.get("address").toString());
            }
            else
                return null;
        }
        return null;
    }

    @Override
    public Boolean register(String name, String pwd) {
        int i;
        String sql = "insert into users (username,userpwd,gendar,uimage,age,address) values ('"+name+"','"+pwd+"',0,11,1,11)";
        i = DBHelper.executeUpdata(sql);
        if (i<=0)
            return false;
        else
            return true;
    }
}
